package com.meida.module.bus.provider.api.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 模拟soapUI调用WebService,解析返回报文
 *
 */
public class Soap_Util {
    //测试环境地址
//    public static String INVOICE_WS_URL = "http://10.51.1.83:22000/qad2018ee_webservice/services/master";
	public static void main(String[] args) throws Exception {
		String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"webservice.xuch.interfaces.weaver.com.cn\">\r\n" +
				"   <soapenv:Header/>\r\n" +
				"   <soapenv:Body>\r\n" +
				"      <web:test>\r\n" +
				"         <web:in0>{\r\n" +
				"\"workflowid\":\"813\",\r\n" +
				"\"userNum\":\"00325\",\r\n" +
				"\"workflowname\":\"PMS测试\",\r\n" +
				"\"xmjlgh\":\"00325\",\r\n" +
				"\"approvalUrl\":\"http://www.baidu.com\",\r\n" +
				"\"approvalDocNm\":\"ceshi\",\r\n" +
				"\"applydate\":\"2020-10-24\"\r\n" +
				"}</web:in0>\r\n" +
				"      </web:test>\r\n" +
				"   </soapenv:Body>\r\n" +
				"</soapenv:Envelope>";
		String url ="http://218.90.189.210:8889/services/UseridsWebService";
		GetDate(xml,url);
	}

    public static void GetDate(String xml,String url) throws Exception {

        StringBuffer stringBuffer = testWebService(xml,url);
		System.out.println("xml::"+xml);
        //处理返回数据
        String xmlResult = stringBuffer.toString().replace("<", "<");
        System.out.println("xmlResult:"+xmlResult);
    }

    // 调用WS
    private static StringBuffer testWebService(String sendMsg,String INVOICE_WS_URL) throws Exception {
        //拼接请求报文
        System.out.println("sendMsg:"+sendMsg);
        // 开启HTTP连接ַ
        InputStreamReader isr = null;
        BufferedReader inReader = null;
        StringBuffer result = null;
        OutputStream outObject = null;
        try {
            URL url = new URL(INVOICE_WS_URL);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

            // 设置HTTP请求相关信息
            httpConn.setRequestProperty("Content-Length",
                    String.valueOf(sendMsg.getBytes().length));
            httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);

            // 进行HTTP请求
            outObject = httpConn.getOutputStream();
            outObject.write(sendMsg.getBytes());

            if (200 != (httpConn.getResponseCode())) {
                throw new Exception("HTTP Request is not success, Response code is " + httpConn.getResponseCode());
            }
            // 获取HTTP响应数据
            isr = new InputStreamReader(
                    httpConn.getInputStream(), "utf-8");
            inReader = new BufferedReader(isr);
            result = new StringBuffer();
            String inputLine;
            while ((inputLine = inReader.readLine()) != null) {
                result.append(inputLine);
            }
            return result;

        } catch (IOException e) {
            throw e;
        } finally {
            // 关闭输入流
            if (inReader != null) {
                inReader.close();
            }
            if (isr != null) {
                isr.close();
            }
            // 关闭输出流
            if (outObject != null) {
                outObject.close();
            }
        }

    }

}
